package com.zt.bus.line.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.bus.line.model.Dispatch;
import com.zt.bus.line.service.DispatchService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/dispatch")
public class DispatchController {

	@Autowired
	private DispatchService dispatchService;

	@RequestMapping(value = "/add")
	@ResponseBody
	public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		String lineId = reqBean.getInfo("lineId");
		String planId = reqBean.getInfo("planId");
		String lineName = reqBean.getInfo("lineName");
		Date departDate = reqBean.getInfoDate("departDate");
		String userId = reqBean.getUserId();  
		
		JSONArray driverArray =JSONArray.fromObject(reqBean.getObjectInfo("drivers"));// 分配的司机
		JSONArray vehicleArray =JSONArray.fromObject(reqBean.getObjectInfo("vehicles"));// 分配的车辆
		
		dispatchService.addDispatch(lineId,planId,lineName,departDate,userId,driverArray,vehicleArray);
		return Response.responseSuccess();
	}
	
	@RequestMapping(value = "/getByPlanIdAndDepartDate")
	@ResponseBody
	public JSONObject getByPlanIdAndDepartDate(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		String planId = reqBean.getInfo("planId");
		Date departDate = reqBean.getInfoDate("departDate");
		
		List<Dispatch> list = dispatchService.getByPlanIdAndDepartDate(planId,departDate);
		JSONArray planDriverArray = new JSONArray();
		for (Dispatch dispatch : list) {
			planDriverArray.add(dispatch.toJson(dispatch));
		}
		return Response.response(planDriverArray);
	}
}
